package com.itheima.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentOption;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceimpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;


    @Autowired
    private ClazzServiceimpl clazzService;


    @Override
    public PageResult<Student> getStudent(String name, String degree, Integer clazzId , Integer page, Integer pageSize) {
//        //设置分页查询
//        PageHelper.startPage(page, pageSize);
//        //调用name, degree , clazzId数据
//        List<Student> students = studentMapper.findAll(name, degree , clazzId);
//        Page p = (Page)students;
//        //封装实体类
//        PageResult<Student> pageResult = new PageResult<>();
//        //获取所有的学生人数
//        pageResult.setTotal(p.getTotal());
//        //获取所有的数据信息
//        pageResult.setRows(p.getResult());
//        return pageResult;
        //设置分页查询
        IPage<Student> _page = new Page<>(page, pageSize);
         //查询条件
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty( name),Student::getName, name);
        queryWrapper.like(!ObjectUtils.isEmpty( degree),Student::getDegree, degree);
        queryWrapper.eq(!ObjectUtils.isEmpty( clazzId),Student::getClazzId, clazzId);
            //查询所有数据
        _page = super.page(_page , null);

       List<Student> students = _page.getRecords();
//        for (Student student : students) {
//            //查询班级名称
//            Clazz clazz = clazzService.queryClazz(student.getId());
//            if (clazz != null){
//                student.setClazzName(clazz.getName());
//            }
//        }
        return new PageResult<>(_page.getTotal(), students);
    }

    @Override
    public void deleteStudent(List<Integer> ids) {
        //删
        studentMapper.deleteStudent(ids);
    }

    @Override
    public void postStudent(Student student) {
        studentMapper.postStudent(student);

    }
    //数据回显
    @Override
    public Student getStudentById(Integer id) {
     Student student = studentMapper.getStudentById(id);
     return student;
    }
    //更新数据
    @Override
    public void putStudent(Student student) {
        studentMapper.putStudent(student);
    }
    //违纪处理
    @Override
    public void disposeById(Integer id, Integer score) {
        studentMapper.disposeById(id , score);
    }
    /*学历统计 , 这里我们由接口文档不难看出, 前端要的数据是一个集合里面装着两个数据
               [{name = ? , values = ?}
               [{name = ? , values = ?}]
               但是前端所需要的是就是这种两个数据在一个集合中因此我们不需要再将他们分开, 没有对象名因此也不需要封装, 直接返回这种list集合嵌套的map集合即可
     */

    @Override
    public List<Map<String, Object>> stuentDegreeDate() {
        return studentMapper.stuentDegreeDate();
    }
    /*统计人数, 因为班级的不同所以每个班级的人数都是不同的, 我们应该,使用MAP集合将他们形成两个键值对
    list(
                        {name = JavaEE90期 , values = 3}
                        {name = JavaEE166期 , values = 8}
                        {name = JavaEE167期 , values = 15}
     从这里可以看出我们完全可以形成list集合去存储
    )
     */

    @Override
    public StudentOption studentCountData() {
        List<Map<Student , Object>> list = studentMapper.studentCountData();
        List<Object> studentList = list.stream().map((Map<Student, Object> map) -> {
            Object student = map.get("className");
            return student;
        }).collect(Collectors.toList());

        List<Object> numberList = list.stream().map((Map<Student, Object> map) -> {
            Object number = map.get("number");
            return number;
        }).collect(Collectors.toList());

        StudentOption studentOption = new StudentOption();
        studentOption.setClazzList(studentList);
        studentOption.setDataList(numberList);
        return studentOption;
    }
}

